const vscode = require('vscode');
//const util = require('./util');

/**
 * 自动提示实现，这里模拟一个很简单的操作
 * 当输入 this.dependencies.xxx时自动把package.json中的依赖带出来
 * 当然这个例子没啥实际意义，仅仅是为了演示如何实现功能
 * @param {*} document 
 * @param {*} position 
 * @param {*} token 
 * @param {*} context 
 */
function provideCompletionItems(document, position, token, context) {
    const line = document.lineAt(position);
    console.log("进入自动补全")
    // 只截取到光标位置为止，防止一些特殊情况
    const lineText = line.text.substring(0, position.character);
    
    //一下为所有的补全代码
    const R_HtmlStr = RegExp('htmlstr','gi')//注册一个正则表达式
    if (R_HtmlStr.test(lineText)) {
        const dependencies = ["AColNo","AddTag","AtoCheck","AtoFile","AtoHyperlink","AtoList","AtoRadio","AtoText","AtoTrTd","GetBodyText","ImgBase64","ImgInline","StoCheck","StoFile","StoHyperlink","StoList","StoRadio","StoText","StoTrTd","WithEmpty"];
        var xxx = dependencies;
        console.log("返回补全列表")
        console.log(xxx)
        return dependencies.map(dep => {
            // vscode.CompletionItemKind 表示提示的类型
            return new vscode.CompletionItem(dep, vscode.CompletionItemKind.Field);
        })
    }


    const R_ObjTool = RegExp('ObjTool','gi')//注册一个正则表达式
    if (R_ObjTool.test(lineText)) {
        const dependencies = ["Add","AddList","AddNewObj","AddProperties","Copy","Find","GetHTML","NewEmpty","NewProperties","Scx2Obj"];
        var xxx = dependencies;
        console.log("返回补全列表")
        console.log(xxx)
        return dependencies.map(dep => {
            // vscode.CompletionItemKind 表示提示的类型
            return new vscode.CompletionItem(dep, vscode.CompletionItemKind.Field);
        })
    }

    const R_oSV = RegExp('oSV','gi')//注册一个正则表达式
    if (R_oSV.test(lineText)) {
        const dependencies = ["InnerIP","IP","IP1","IP12","IP123","IP1N","IP2","IP23","IP234","IP2N","IP3","IP34","IP3N","IP4","IP4N","Remote_Addr","Url"];
        var xxx = dependencies;
        console.log("返回补全列表")
        console.log(xxx)
        return dependencies.map(dep => {
            // vscode.CompletionItemKind 表示提示的类型
            return new vscode.CompletionItem(dep, vscode.CompletionItemKind.Field);
        })
    }

    const R_Request = RegExp('Request','gi')//注册一个正则表达式
    if (R_Request.test(lineText)) {
        const dependencies = ["BinaryRead","Cookie","CookieArray","CookieObj","Decode","Form","FormArray","FormCount","FormObj","GetCookie","Item","QueryString","QueryStringArray","QueryStringCount","QueryStringObj","ServerVariables","ServerVariablesArr","ServerVariablesObj","TotalBytes","UpBig","Upload"];
        var xxx = dependencies;
        console.log("返回补全列表")
        console.log(xxx)
        return dependencies.map(dep => {
            // vscode.CompletionItemKind 表示提示的类型
            return new vscode.CompletionItem(dep, vscode.CompletionItemKind.Field);
        })
    }

    const R_Response = RegExp('Response','gi')//注册一个正则表达式
    if (R_Response.test(lineText)) {
        const dependencies =["AddHeader","AppendTolog","AutoHead","BaseProperty","BinaryWrite","BodyOther","BodyProperty","BodyScript","Buffer","CacheControl","CharSet","Clear","ContentType","DelCookie","DocType","Download","End","Expires","ExpiresAbsolute","Flush","FunScript","HeadOther","InsScript","IsClientConnected","Link","JavaScript","Meta","MetaView","OnLoad","OtherTags","Redirect","Script","SetCookie","SrcScript","Status","Style","Title","ToVariable","Utf8","UWrite","VbScript","Write","WriteLn","Refresh"];
        var xxx = dependencies;
        console.log("返回补全列表")
        console.log(xxx)
        return dependencies.map(dep => {
            // vscode.CompletionItemKind 表示提示的类型
            return new vscode.CompletionItem(dep, vscode.CompletionItemKind.Field);
        })
    }

    const R_Server = RegExp('Server','gi')//注册一个正则表达式
    if (R_Server.test(lineText)) {
        const dependencies =["EasyEncode","HtmlDecode","HtmlEncode","JsCompress","JsDecode","JsEncode","MapPath","ScriptTimeout","UrlDecode","UrlEncode","CreateObject"];
        var xxx = dependencies;
        console.log("返回补全列表")
        console.log(xxx)
        return dependencies.map(dep => {
            // vscode.CompletionItemKind 表示提示的类型
            return new vscode.CompletionItem(dep, vscode.CompletionItemKind.Field);
        })
    }

    const R_Session = RegExp('Session','gi')//注册一个正则表达式
    if (R_Session.test(lineText)) {
        const dependencies =["GetSessionID","GetVar","SetVar","Timeout"];
        var xxx = dependencies;
        console.log("返回补全列表")
        console.log(xxx)
        return dependencies.map(dep => {
            // vscode.CompletionItemKind 表示提示的类型
            return new vscode.CompletionItem(dep, vscode.CompletionItemKind.Field);
        })
    }

    const R_UBB = RegExp('UBB','gi')//注册一个正则表达式
    if (R_UBB.test(lineText)) {
        const dependencies =["GetSessionID","GetVar","SetVar","Timeout"];
        var xxx = dependencies;
        console.log("返回补全列表")
        console.log(xxx)
        return dependencies.map(dep => {
            // vscode.CompletionItemKind 表示提示的类型
            return new vscode.CompletionItem(dep, vscode.CompletionItemKind.Field);
        })
    }

    const R_Vfpweb = RegExp('Vfpweb','gi')//注册一个正则表达式
    if (R_Vfpweb.test(lineText)) {
        const dependencies =["AddFromArray","Ar2Cursor","Ar2Html","BlankType","ClearPublic","ColGroup","ColPArray","DataPath","Dbf2Html","Dbf2Json","Delay","DllPath","ErrContinue","ErrFile","ErrMsg","ErrVarList","ExecString","FromCamel","IIS","IpDebug","JsonLogic","JsonQuote","Json2Array","Obj2Json","PinYin","PinYin0","PY","PY0","RetryCount","SearchStr","SetPath","Stroke","TableCaption","TableProperty","TdEArray","TdEList","TdPArray","TdPList","TdVArray","ThPArray","ThPList","ThTArray","ThTList","ToArray","ToCamel","ToHtml","ToType","TrPArray","TrPList","TrProperty","Txt2Array","Ver","WebPath","WorkPath","xAt","Public","xReplace","Sort","Use"];
        var xxx = dependencies;
        console.log("返回补全列表")
        console.log(xxx)
        return dependencies.map(dep => {
            // vscode.CompletionItemKind 表示提示的类型
            return new vscode.CompletionItem(dep, vscode.CompletionItemKind.Field);
        })
    }

    const R_xlstool = RegExp('xlstool','gi')//注册一个正则表达式
    if (R_xlstool.test(lineText)) {
        const dependencies =["Ar2Sheet","FontName","FontSize","InnerLineWidth","ObjBind","oExcel","oSheet","OuterLineWidth","oWorkbook","SetPage","TitleFontName","TitleFontSize","xlClose","xlErrMsg","xlErrNo","XlOpen","Sheet2Array","xlQuit"];
        var xxx = dependencies;
        console.log("返回补全列表")
        console.log(xxx)
        return dependencies.map(dep => {
            // vscode.CompletionItemKind 表示提示的类型
            return new vscode.CompletionItem(dep, vscode.CompletionItemKind.Field);
        })
    }
    
    const R_ddd = RegExp('ddd','gi')//注册一个正则表达式
    if (R_ddd.test(lineText)) {
        const dependencies =["Kaaa","Vaaa"];
        ///var xxx = dependencies;
        console.log("返回补全列表")
        console.log(xxx)
        return dependencies.map(dep => {
            // vscode.CompletionItemKind 表示提示的类型
            //return new vscode.CompletionItem(dep, vscode.CompletionItemKind.Field);
            var xxx =  [{label:dep,insertText:dep,detail:dep}]
            return new vscode.CompletionItem(dep, vscode.CompletionItemKind.Field);
        })
    }


}

/**
 * 光标选中当前自动补全item时触发动作，一般情况下无需处理
 * @param {*} item 
 * @param {*} token 
 */
function resolveCompletionItem(item, token) {
    console.log(item);
    return null;
}

module.exports = function (context) {
    // 注册代码建议提示，只有当按下“.”时才触发
    context.subscriptions.push(vscode.languages.registerCompletionItemProvider('foxpro', {
        provideCompletionItems,
        resolveCompletionItem
    }, '.'));
};

